//给你一个二叉树的根节点 root ， 检查它是否轴对称。 
//
// 
//
// 示例 1： 
// 
// 
//输入：root = [1,2,2,3,4,4,3]
//输出：true
// 
//
// 示例 2： 
// 
// 
//输入：root = [1,2,2,null,3,null,3]
//输出：false
// 
//
// 
//
// 提示： 
//
// 
// 树中节点数目在范围 [1, 1000] 内 
// -100 <= Node.val <= 100 
// 
//
// 
//
// 进阶：你可以运用递归和迭代两种方法解决这个问题吗？ 
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 2459 👎 0


package leetcode.editor.cn;

// [101]对称二叉树

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.TreeMap;

public class SymmetricTree_101 {
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Solution solution = new SymmetricTree_101().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null) {
                return false;
            }
            if (root.left == null && root.right == null) {
                return true;
            }
            LinkedList<TreeNode> queue = new LinkedList<>();
            if (root.left != null) {
                queue.add(root.left);
            }
            if (root.right != null) {
                queue.add(root.right);
            }
            while (!queue.isEmpty()) {
                int size = queue.size();
                if ((size & 1) != 0) {
                    return false;
                }
                ArrayList<TreeNode> list = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    list.add(node);
                    if (node != null) {
                        queue.add(node.left);
                        queue.add(node.right);
                    }
                }
                for (int i = 0; i < size / 2; i++) {
                    if (!isSame(list.get(i), list.get(size - 1 - i))) {
                        return false;
                    }
                }
            }
            return true;
        }

        private boolean isSame(TreeNode a, TreeNode b) {
            if (a == null && b == null) {
                return true;
            }
            return a != null && b != null && a.val == b.val;
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}